<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Guard_model extends CI_Model {
	function __construct(){
        parent::__construct();
    }
    
    function add($data = array()){
    	$this->db->insert('rsh_guard', $data);
		$guard_id = $this->db->insert_id();
		return $guard_id;
    }
    
 	function update($data,$where){
    	$this->db->where($where);
		$query = $this->db->update('rsh_guard', $data);
		$res = $this->db->affected_rows();
		return $res;
    }
    
      /**
	 * 站岗宝转出审核
	 * @param $data
	 */
    function guard_verify_out($data){
		$id = $data['id'];
		$guard = self::get_guard_turninto_obj($id);
		if ($guard['fstatus']>0){
			return "此活利宝已经审核过，不能再审核";
		}
		
		$this->load->model('Account_model');
		$ftatus = $data['fstatus'];
		$user_id = $guard['user_id'];
		$mode = substr(strval($user_id),-1,1);
		$user_account = $this->Account_model->get_account($user_id);
		
		$money = $guard['money'];
		if($ftatus == 1){
			$log['user_id'] 		= $user_id;
			$log['ftype'] 			= OUT_GUARD;
			$log['money'] 			= $money;
			$log['total'] 			= $user_account['total']+$log['money'];
			$log['use_money'] 		= $user_account['use_money']+$log['money'];
			$log['no_use_money'] 	= $user_account['no_use_money'];
			$log['collection'] 		= $user_account['collection'];
			$log['to_user'] 		= 1;
			$log['remark'] 			= "从活利宝转出来的资金";
			$log['add_time'] 		= time();
			$log['addip'] 			= $data['addip'];
			$this->db->insert('rsh_account_log_'.$mode, $log);
			//更新用户资金表
			$this->db->query("update rsh_account set total=total+{$money},use_money=use_money+{$money},huolibao_total=huolibao_total-{$money} where user_id={$user_id}");
			
		}
		
		$time = time();
		$this->db->query("update rsh_guard set fstatus={$ftatus},verify_time={$time} where id={$guard['id']}");
		return 1;
		
		
    }
    
    /**
	 * 站岗宝转出
	 * @param $data
	 */
    function repayment($data = array()){
		$tender_id = self::add($data);
		$user_id = $data['user_id'];
		$money = $data['money'];
		if($tender_id > 0){		
			return 1;
		}
		return 0;
    }
    
    
    
    
    /**
	 * 站岗宝投资
	 * @param $data
	 */
    function invest($data){
    	
		$user_id = $data['user_id'];
		$money = $data['money'];
		$tender_id = self::add($data);
		if ($tender_id > 0){
			$this->load->model('Account_model');
			
			$this->db->trans_start();
			
			$user_account = $this->Account_model->get_account($user_id);
			$log['user_id'] 		= $user_id;
			$log['ftype'] 			= INVEST_GUARD;
			$log['money'] 			= $money;
			$log['total'] 			= $user_account['total'];
			$log['use_money'] 		= $user_account['use_money']-$log['money'];
			$log['no_use_money'] 	= $user_account['no_use_money']+$log['money'];
			$log['collection'] 		= $user_account['collection'];
			$log['to_user'] 		= 1;
			$log['op'] 		= 1;
			$log['remark'] 			= "转入活利宝资金冻结";
			$log['add_time'] 		= time();
			$log['addip'] 			= $data['addip'];
			$mode = substr(strval($user_id),-1,1);
			$this->db->insert('rsh_account_log_'.$mode, $log);
			//更新用户资金表
			$this->db->query("update rsh_account set use_money=use_money-{$money},no_use_money=no_use_money+{$money} where user_id={$user_id}");
			$this->db->trans_complete();
			if ($this->db->trans_status() === TRUE){
				return 1;
			}else{
				return 0;
			}
		}else{
			return false;
		}
    }
    
    function get_guard_turninto_obj($id){
    	$select = "*";
		$this->db->select($select);
		$this->db->where('id', $id);
		$query = $this->db->get('rsh_guard');
		return $query->row_array();
    }
    
  	 /**
	 * 站岗宝审核转入
	 * @param $data
	 */
    function guard_verify_into($data){    	
		$id = $data['id'];
		$guard = self::get_guard_turninto_obj($id);
		if ($guard['fstatus']>0){
			return "此活利宝已经审核过，不能再审核";
		}
		$this->load->model('Account_model');
		
		$ftatus = $data['fstatus'];
		$mode = $guard['user_id']%100;
		$user_account = $this->Account_model->get_account($guard['user_id']);
		if($ftatus == 1){
			//扣除冻结金额
			
			$log['user_id'] 		= $user_account['user_id'];
			$log['ftype'] 			= GUARD;//扣除冻结款
			$log['money'] 			= $guard['money'];
			$log['total'] 			= $user_account['total']-$log['money'];
			$log['use_money'] 		= $user_account['use_money'];
			$log['no_use_money'] 	= $user_account['no_use_money']-$log['money'];
			$log['collection'] 		= $user_account['collection'];
			$log['to_user'] 		= 1;
			$log['remark'] 			= "活利宝转入费用扣除";
			$log['add_time'] 		= time();
			$log['addip'] 			= $data['addip'];
			$this->db->insert('rsh_account_log_'.$mode, $log);
			$this->db->query("update rsh_account set total=total-{$log['money']},no_use_money=no_use_money-{$log['money']},huolibao_total=huolibao_total+{$log['money']} where user_id={$guard['user_id']}");
		}elseif($ftatus == 2){
			//返回投资者的钱
			$log['user_id'] 		= $user_account['user_id'];
			$log['ftype'] 			= GUARD_FALSE;//扣除冻结款
			$log['money'] 			= $guard['account'];
			$log['total'] 			= $user_account['total'];
			$log['use_money'] 		= $user_account['use_money']+$log['money'];
			$log['no_use_money'] 	= $user_account['no_use_money']-$log['money'];
			$log['collection'] 		= $user_account['collection'];
			$log['to_user'] 		= 1;
			$log['remark'] 			= "活利宝转入审核不通过";
			$log['add_time'] 		= time();
			$log['addip'] 			= $data['addip'];
			$this->db->insert('rsh_account_log_'.$mode, $log);
			$this->db->query("update rsh_account set use_money=use_money+{$log['money']},no_use_money=no_use_money-{$log['money']} where user_id={$guard['user_id']}");
		}
		
		$time = time();
		$this->db->query("update rsh_guard set fstatus={$ftatus},verify_time={$time} where id={$guard['id']}");
		return 1;
    }
    
    
    /**
	 * 统计转入转出累计收益金额
	 */
    function total_guard($data=array()){
    	
		$_where = " ";
    	if (isset($data['user_id']) && $data['user_id'] != ''){
			$_where = " and user_id = ".$data['user_id'];
		}
		
    	if(isset($data['addtime']) && $data['addtime'] != 0){
			$_where .= " and add_time >= ".$data['addtime'];
		}
		
		
	
    	//转入金额
		$sql_into_sum = "select sum(money) as count from rsh_guard where ftype=1 ".$_where;
		$query_into = $this->db->query($sql_into_sum);
		$data['into_money_count'] = $query_into->row()->count;
		
		//转出金额
		$sql_out_sum = "select sum(money) as count from rsh_guard where ftype=2 ".$_where;
		$query_out = $this->db->query($sql_out_sum);
		$data['out_money_count'] = $query_out->row()->count;
		
		//累计收益金额
		$sql_log_sum = "select sum(interest) as count from rsh_guard_log where 1=1 ".$_where;
		$query_log = $this->db->query($sql_log_sum);
		$data['log_money_count'] = $query_log->row()->count;
		
		return $data;
		
    }
    
    /**
	 * 转入明细
	 */
    function get_guard_into_list($data){
    	
    	$_where = " where 1=1 ";
    	
    	if (isset($data['user_id']) && $data['user_id']>0){
    		$_where .=" and p1.user_id='{$data['user_id']}'";
    	}
    	
    	if (isset($data['username']) && $data['username']!=''){
    		$_where .=" and p2.username='{$data['username']}'";
    	}
    	if (isset($data['fstatus']) && $data['fstatus']>0){
    		$_where .=" and p1.fstatus=".$data['fstatus'];
    	}else{
    		$_where .=" and p1.fstatus=0";
    	}    	
    	
   	 	if (isset($data['ftype']) && $data['ftype']>0){
    		$_where .=" and p1.ftype='{$data['ftype']}'";
    	}
    	
    	$_order = " order by p1.fstatus asc,add_time desc";
    	
    	$_limit = "";
    	if (isset($data['per_page']) && isset($data['page_size'])){
    		$_limit = " limit {$data['per_page']},{$data['page_size']}";
    	}
    	
    	$sql = "select p1.*,p2.username from rsh_guard as p1 
    		left join rsh_user as p2 on p1.user_id=p2.user_id ".$_where.$_order.$_limit;
		
		$query = $this->db->query($sql);
		$list = $query->result_array();
		
   	 	
		$sql_count = "select count(p1.id) as count from rsh_guard as p1 
    		left join rsh_user as p2 on p1.user_id=p2.user_id ".$_where;
    	$query_count = $this->db->query($sql_count);
		$count = $query_count->row()->count;
		
    	return array(
			'count' => $count,
			'list' => $list
		);
		
    }
    
     /**
	 * 收益明细
	 */
	function get_guard_log_list($data){
		$_where = " where 1=1 ";
		
		if (isset($data['user_id']) && $data['user_id']>0){
    		$_where .=" and p1.user_id='{$data['user_id']}'";
    	}
    	
    	if (isset($data['username']) && $data['username']!=''){
    		$_where .=" and p2.username='{$data['username']}'";
    	}
    	
    	$_order = " order by add_time desc";
    	
    	$_limit = "";
    	if (isset($data['per_page']) && isset($data['page_size'])){
    		$_limit = " limit {$data['per_page']},{$data['page_size']}";
    	}
    	
    	$sql = "select p1.*,p2.username from rsh_guard_log as p1 
    		left join rsh_user as p2 on p1.user_id=p2.user_id ".$_where.$_order.$_limit;
		
		$query = $this->db->query($sql);
		$list = $query->result_array();
		
   	 	
		$sql_count = "select count(p1.id) as count from rsh_guard_log as p1 
    		left join rsh_user as p2 on p1.user_id=p2.user_id ".$_where;
    	$query_count = $this->db->query($sql_count);
		$count = $query_count->row()->count;
		
    	return array(
			'count' => $count,
			'list' => $list
		);
		
	}
    
    
    
    
}

?>